home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1989 / 07 / porter.lst < prev    next >
File List  |  1989-06-01  |  10KB  |  317 lines

  1. _GRAPHICS PROGRAMMING COLUMN_
  2. by Kent Porter
  3.  
  4. [LISTING ONE]
  5.  
  6.  
  7. /* VCOORDS.C: Implements virtual coordinates in GRAFIX library */
  8. /* K. Porter, DDJ Graphics Programming Column, July '89 */
  9.  
  10. #include <math.h>
  11. #include "grafix.h"
  12.  
  13. /* Variables for this compile unit */
  14. double xf = 1.0, yf = 1.0;                 /* x, y conversion factors */
  15. double ox = 0.0, oy = 0.0;                     /* virtual x, y origin */
  16. /* ------------------------------------------------------------------ */
  17. /* set virtual coordinate space */
  18. void far setcoords (double left, double top, double right, double bottom)
  19. {
  20.   xf = (double) vp_width() / (right - left);    /* conversion factors */
  21.   yf = (double) vp_height() / (bottom - top);
  22.   ox = (double) vp_width() - (right * xf);                  /* origin */
  23.   oy = (double) vp_height() - (bottom * yf);
  24. } /* ---------------------------------------------------------------- */
  25.  
  26. int far dx (double vx)               /* convert virtual x to device x */
  27. {
  28.   return (xf * vx) + ox;
  29. } /* ---------------------------------------------------------------- */
  30.  
  31. int far dy (double vy)               /* convert virtual y to device y */
  32. {
  33.   return (yf * vy) + oy;
  34. } /* ---------------------------------------------------------------- */
  35.  
  36. int far dxunits (double vx)                  /* device x units for vx */
  37. {
  38.   return (int)(fabs)(xf * vx);
  39. } /* ---------------------------------------------------------------- */
  40.  
  41. int far dyunits (double vy)                  /* device y units for vy */
  42. {
  43.   return (int)(fabs)(yf * vy);
  44. } /* ---------------------------------------------------------------- */
  45.  
  46.  
  47. [LISTING TWO]
  48.  
  49. Caption: Add this to your copy of GRAFIX.H
  50.  
  51. /* From July, '89 */
  52. /* -------------- */
  53. void far setcoords                  /* set virtual coordinate space */
  54.         (double left, double top, double right, double bottom);
  55.  
  56. int far dx (double vx);            /* convert virtual x to device x */
  57.  
  58. int far dy (double vy);            /* convert virtual y to device y */
  59.  
  60. int far dxunits (double vx);               /* device x units for vx */
  61.  
  62. int far dyunits (double vy);               /* device y units for vy */
  63.  
  64.  
  65.  
  66.  
  67.  
  68. [LISTING THREE]
  69.  
  70. /* TRIGPLOT.C: Plot sine, cosine, and their sum */
  71. /* X axis is integral, Y axis is floating-point */
  72.  
  73. #include <math.h>
  74. #include <conio.h>
  75. #include "grafix.h"
  76. #define DEG2RAD 3.1415927 / 180.0    /* degrees to radians */
  77.  
  78. int px = 999, py;                    /* previous point */
  79.  
  80. void main ()
  81. {
  82. void   plot (int angle, double (*fcn)(double theta));
  83. double sum (double);
  84. int    angle;
  85.  
  86.   if (init_video (EGA)) {
  87.  
  88.     /* Scale the axes */
  89.     setcoords (-360, 1.5, 360, -1.5);
  90.  
  91.     /* Draw the registration lines */
  92.     set_color1 (2);                               /* green  */
  93.     hline (dx(-360), dy(0), dxunits (720));       /* x axis */
  94.     draw_line (dx(0), dy(1.3), dx(0), dy(-1.3));  /* y axis */
  95.     set_color1 (1);                               /* blue   */
  96.     hline (dx(-360), dy(1), dxunits (720));       /* +1.0   */
  97.     hline (dx(-360), dy(-1),dxunits (720));       /* -1.0   */
  98.  
  99.     /* Plot the sine curve */
  100.     set_color1 (3);                               /* cyan   */
  101.     for (angle = -360; angle <= 360; angle++)
  102.       plot (angle, sin);
  103.  
  104.     /* Plot the cosine curve */
  105.     set_color1 (5);                               /* magenta */
  106.     px = 999;                                     /* new curve */
  107.     for (angle = -360; angle <= 360; angle++)
  108.       plot (angle, cos);
  109.  
  110.     /* Plot the sum of sine and cosine */
  111.     set_color1 (15);                              /* white */
  112.     px = 999;                                     /* new */
  113.     for (angle = -360; angle <= 360; angle++)
  114.       plot (angle, sum);
  115.  
  116.     /* Wait for keypress and quit */
  117.     getch();
  118.   }
  119. } /* ------------------------ */
  120.  
  121. double sum (double theta)
  122. {
  123.   return (sin (theta) + cos (theta));
  124. } /* ------------------------ */
  125.  
  126. void plot (int angle, double (*fcn)(double theta))
  127. {
  128. double theta, y;
  129.  
  130.   theta = (double) angle * DEG2RAD;      /* convert to radians */
  131.   y = (*fcn) (theta);                    /* derive value       */
  132.   if (px != 999)                         /* if not first point */
  133.     draw_line (px, py, dx (angle), dy (y));
  134.   px = dx (angle);                       /* save current point */
  135.   py = dy (y);
  136. } /* ------------------------ */
  137.  
  138.  
  139.  
  140.  
  141.  
  142. [LISTING FOUR]
  143.  
  144.  
  145. /* LENDIST.C: Produces a histogram of line lengths in a text file */
  146. /* Illustrates integral virtual coords, self-scaling bar chart */
  147.  
  148. #include <stdio.h>
  149. #include <string.h>
  150. #include <conio.h>
  151. #include <stdlib.h>
  152. #include "grafix.h"
  153. #define MAXLEN 80              /* max length of a line */
  154. #define MAXCAT MAXLEN / 5      /* number of categories */
  155.  
  156. void main (int argc, char *argv[])
  157. {
  158. double group [MAXCAT];      /* results array */
  159. int    category;            /* grouping by length (0-4, 5-9, etc) */
  160. double maxcount = 0;        /* highest count for any category */
  161. int    color = 1;           /* color of histogram bar */
  162. char   buf [MAXLEN];        /* receiving buffer */
  163. FILE   *fp;                 /* text file pointer */
  164.  
  165.   /* check command line for a filename */
  166.   if (argc < 2) {
  167.     puts ("Usage: LENDIST <filename.ext>");
  168.     exit (EXIT_FAILURE);
  169.   }
  170.  
  171.   /* clear the length array */
  172.   for (category = 0; category < MAXCAT; category++)
  173.     group [category] = 0.0;
  174.  
  175.   /* pass thru the file getting lengths of lines */
  176.   if ((fp = fopen (argv[1], "r")) == NULL) {
  177.     printf ("Unable to open %s\n", argv[1]);
  178.     exit (EXIT_FAILURE);
  179.   } else {
  180.     while (fgets (buf, MAXLEN, fp)) {
  181.       category = strlen (buf) / 5;     /* group 0-4, 5-9, etc. */
  182.       ++group [category];              /* count */
  183.     }
  184.     fclose (fp);
  185.   }
  186.  
  187.   /* find highest count of any group */
  188.   for (category = 0; category < MAXCAT; category++)
  189.     if (group [category] > maxcount)
  190.       maxcount = group [category];
  191.  
  192.   /* Normalize groupings to 100 */
  193.   for (category = 0; category < MAXCAT; category++)
  194.     group [category] /= (maxcount / 100);
  195.  
  196.   /* enter graphics mode */
  197.   if (init_video (EGA)) {
  198.  
  199.     /* scale the histogram width */
  200.     /* leave space on top and left for text */
  201.     setcoords (-22, -2, 100, 23);
  202.  
  203.     /* label the histogram */
  204.     printf ("Distribution of line lengths in %s\n\n", argv[1]);
  205.     for (category = 0; category < MAXCAT; category++)
  206.       printf (" Length %2d-%2d:\n", category*5, (category*5)+4);
  207.  
  208.     /* set up the graph area */
  209.     draw_rect (dx(-22), dy (-1), dxunits (122),
  210.                dyunits(MAXCAT + 2));
  211.     set_color1 (1);   /* blue */
  212.     for (category = 0; category < 100; category+=10)
  213.       draw_line (dx(category), dy(-0.5), dx(category), dy(MAXCAT));
  214.  
  215.     /* draw the histogram bars */
  216.     for (category = 0; category < MAXCAT; category++) {
  217.       set_color1 (color);
  218.       if (++color == 16) color = 1;
  219.       fill_rect (dx (0), dy (category), dxunits (group [category]),
  220.                  dyunits (0.75));
  221.     }
  222.  
  223.     /* hold for keypress, then quit */
  224.     getch();
  225.     pc_textmode();
  226.   } else
  227.     puts ("Unable to enter EGA graphics");
  228. }
  229.  
  230.  
  231.  
  232. [LISTING FIVE]
  233.  
  234. /* RESIZE.C: Effects of resizing virtual coords to fit viewports */
  235.  
  236. #include "grafix.h"
  237. #include <conio.h>
  238. #include <stdio.h>
  239. #define  BORDER 15
  240.  
  241. void main ()
  242. {
  243. void set_vp (int, int, int, int);
  244.  
  245.   if (init_video (EGA)) {
  246.  
  247.     /* Set colors for shading */
  248.     set_ega_palreg (1, ega_blend (RED3, GRN1, BLU0));
  249.     set_ega_palreg (2, ega_blend (RED2, GRN1, BLU0));
  250.     set_ega_palreg (3, ega_blend (RED2, GRN0, BLU0));
  251.  
  252.     /* Draw 4-pointed star in variously-sized viewports */
  253.     set_vp (1, 1, 160, 120);          /* square */
  254.     set_vp (200, 1, 120, 347);        /* tall and skinny */
  255.     set_vp (360, 270, 238, 78);       /* short and fat */
  256.  
  257.     /* Wait for keypress and quit */
  258.     getch();
  259.     pc_textmode();
  260.   } else
  261.     puts ("Unable to enter EGA graphics");
  262. } /* ---------------------------------- */
  263.  
  264. void set_vp (int left, int top, int width, int height)
  265. {
  266. VP_HAN vp;
  267. void drawstar (void);
  268.  
  269.   vp = vp_open (left, top, width, height);
  270.   set_color1 (BORDER);
  271.   vp_outline (vp);
  272.   setcoords (-120, 120, 120, -120);  /* constants scale to vp size */
  273.   drawstar();
  274.   vp_close (vp);
  275. } /* ---------------------------------- */
  276.  
  277. void drawstar (void)
  278. {
  279.   set_color1 (BORDER);     /* border color */
  280.   setfillborder (BORDER);
  281.   draw_rect (dx(-30), dy(-30), dxunits(60), dyunits(60));
  282.   set_color1 (3);      /* center of star */
  283.   floodfill (dx(0), dy(0));
  284.  
  285.   set_color1 (BORDER);
  286.   draw_line (dx(-30), dy(30), dx(0), dy(100));  /* top ray */
  287.   draw_line (dx(0), dy(100), dx(30), dy(30));
  288.   draw_line (dx(0), dy(100), dx(0), dy(30));
  289.  
  290.   draw_line (dx(-30), dy(30), dx(-100), dy(0));  /* left ray */
  291.   draw_line (dx(-100), dy(0), dx(-30), dy(-30));
  292.   draw_line (dx(-100), dy(0), dx(-30), dy(0));
  293.  
  294.   draw_line (dx(-30), dy(-30), dx(0), dy(-100));  /* bottom ray */
  295.   draw_line (dx(0), dy(-100), dx(30), dy(-30));
  296.   draw_line (dx(0), dy(-100), dx(0), dy(-30));
  297.  
  298.   draw_line (dx(30), dy(-30), dx(100), dy(0));  /* right ray */
  299.   draw_line (dx(100), dy(0), dx(30), dy(30));
  300.   draw_line (dx(100), dy(0), dx(30), dy(0));
  301.  
  302.   set_color1 (1);
  303.   floodfill (dx(10), dy(40));
  304.   floodfill (dx(-40), dy(20));
  305.   floodfill (dx(10), dy(-40));
  306.   floodfill (dx(40), dy(20));
  307.  
  308.   set_color1 (2);
  309.   floodfill (dx(-10), dy(40));
  310.   floodfill (dx(-40), dy(-20));
  311.   floodfill (dx(-10), dy(-40));
  312.   floodfill (dx(40), dy(-20));
  313. }
  314.  
  315.  
  316.  
  317.